package com.john.test.http.proxy.data;

import com.john.http.core.util.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * 〈一句话功能简述〉<br/>
 * 〈功能详细描述〉
 *
 * @author jiangguangtao on 2016/4/28.

 */
public class ProxyItem implements Serializable, Comparable<ProxyItem> {
    public static final Double DEFAULT_SPEED = 0.0;
    public static final Double DEFAULT_DELAY = 100.0;
    private String ip;
    private Integer port;
    private String proxyType;
    private Double speed = DEFAULT_SPEED;
    private Double delay = DEFAULT_DELAY;
    //存活时间 分钟数
    private Long timeToLive = 0L;
    private String location;
    private Date lastValidTime;
    /**
     * 本地测试延迟时间 秒
     */
    private Double localDelay;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getProxyType() {
        return proxyType;
    }

    public void setProxyType(String proxyType) {
        this.proxyType = proxyType;
    }

    public Double getSpeed() {
        return speed;
    }

    public void setSpeed(Double speed) {
        if (null == speed) {
            return;
        }
        this.speed = speed;
    }

    public Double getDelay() {
        return delay;
    }

    public void setDelay(Double delay) {
        if (null == delay) {
            return;
        }
        this.delay = delay;
    }
    public Long getTimeToLive() {
        return timeToLive;
    }

    public void setTimeToLive(Long timeToLive) {
        if (null == timeToLive || timeToLive < 1) {
            return;
        }
        this.timeToLive = timeToLive;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public Date getLastValidTime() {
        return lastValidTime;
    }

    public void setLastValidTime(Date lastValidTime) {
        this.lastValidTime = lastValidTime;
    }

    /**
     * @return 本地测试延迟时间 秒
     */
    public Double getLocalDelay() {
        return localDelay;
    }

    /**
     * @param localDelay 本地测试延迟时间 秒
     */
    public void setLocalDelay(Double localDelay) {
        this.localDelay = localDelay;
    }

    /**
     * 是否是一个有效的条目
     *
     * @return
     */
    public boolean isValidPi() {
        return StringUtils.isNotBlank(ip) && null != port && port > 1;
    }

    public String simpleDesc() {
        return proxyType + ":" + ip + ":" + port;
    }

    @Override
    public String toString() {
        return "ProxyItem{" +
                "ip='" + ip + '\'' +
                ", port=" + port +
                ", proxyType='" + proxyType + '\'' +
                ", speed=" + speed +
                ", delay=" + delay +
                ", timeToLive=" + timeToLive +
                ", location='" + location + '\'' +
                ", lastValidTime=" + DateUtil.format(lastValidTime, DateUtil.yyyyMMddHHmm) +
                '}';
    }

    @Override
    public int compareTo(ProxyItem pi) {
        if (!pi.lastValidTime.equals(this.lastValidTime)) {
            return pi.lastValidTime.compareTo(this.lastValidTime);
        }
        if (!pi.timeToLive.equals(this.timeToLive)) {
            return pi.timeToLive.compareTo(this.timeToLive);
        }
        if (!pi.delay.equals(this.delay)) {
            return this.delay.compareTo(pi.delay);
        }
        if (!this.speed.equals(pi.speed)) {
            return pi.speed.compareTo(this.speed);
        }
        return this.ip.compareTo(pi.ip);
    }
}
